// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ADS } = require("../ads/ADS");
const { videoAds } = require('../ads/video');
const { show, msg } = require("../game/alert");
const { initPrefab, initPrefabPool } = require("../util/gameResources");
const { default: network } = require("../util/network");
cc.Class({
    extends: cc.Component,

    properties: {
      startBtn: cc.Button,
      shopName: cc.EditBox,
      btnLabel: cc.Label,
    },
    mode: 0,
    // LIFE-CYCLE CALLBACKS:
    onLoad() {
        cc.debug.setDisplayStats(false)
        
        initPrefab().then(res => {
            // cc.log(PrefabCache)
            initPrefabPool();
        })
        this.app = cc.cloud && cc.cloud.initialize();
        this.auth = this.app.auth();
        // ADS.initAds();
        let shopTitle = cc.sys.localStorage.getItem('shopList');
        if(shopTitle){
            this.shopName.string = cc.sys.localStorage.getItem('shopName');
            this.btnLabel.string = '继续游戏';
            this.mode = 1;
        }
    },
    start () {

    },
    getStart() {
        if(this.mode) {
            cc.director.loadScene("home");
            return;
        }
        let name = this.shopName.string;
        if(name.length > 4 || name.length <= 0 ){
            show('未来集团','名字太长了，记不住,请保持4字内')
            return ;
        }
        this.auth.anonymousAuthProvider().signIn().then(res => {
            // 需要先做授权才能正常调用。使用匿名登陆方式访问云开发资源
            // 请到腾讯云后台 -> 云开发 -> 选择当前环境 -> 环境设置/登录授权中，确认已经启用匿名登录
            // 匿名登录有一定限制，后期请更换为自定义登录等其他方式。
            console.log('TCB inited');
            this.app.callFunction({
                // 云函数名称，要与上传的函数名一致
                name: "rank",
                // 传给云函数的参数
                data: {
                    action: "register",
                    params: {
                        name: name,
                    }
                }
            }).then(res => {
                console.log('function', res);
                if(res.result.data.length > 0) {
                    show('未来科技','该品牌已存在');
                }else {
                    window.shopName = name;
                    cc.sys.localStorage.setItem('shopName', name );
                    msg('玩命加载中....');
                    cc.director.loadScene('home');
                }
                
            }).catch(console.error);
        });
        
        //查询后台有没有对应的数据
        // network.connect('uid', (host, port) => {
        //     pomelo.request(route, {
        //         username: name,
        //     }, function(data) {
        //         if(data.error) {
        //             showError(DUPLICATE_ERROR);
        //             return;
        //         }
        //         console.log(data);
                
        //     });
        // })
    }
    // update (dt) {},
});
